<html>
<head><meta charset="utf-8"><title>OR traits · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html">OR traits</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212590737"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212590737" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212590737">(Oct 07 2020 at 17:18)</a>:</h4>
<p>Is there any experimental feature that will allow me to make a function that accepts types implementing A OR B?<br>
something like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">f</span><span class="o">&lt;</span><span class="n">T</span>: <span class="nb">Copy</span> <span class="o">||</span><span class="w"> </span><span class="nb">Clone</span><span class="p">(</span><span class="n">t</span>: <span class="nc">T</span><span class="p">){}</span><span class="w"></span>
</code></pre></div>

<p>or even:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">Clonable</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">cloner</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="bp">Self</span>: <span class="nb">Sized</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">C</span>: <span class="nb">Clone</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Clonable</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">cloner</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">&lt;</span><span class="bp">Self</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nb">Clone</span><span class="o">&gt;</span>::<span class="n">clone</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">C</span>: <span class="nb">Copy</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Clonable</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">C</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">cloner</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">*</span><span class="bp">self</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>I know that there's a problem of "which should be used for a type implementing both?" but maybe there's a nightly rust feature that lets you somehow tell the compiler which take precedents(specialization?)</p>
<p>(ignore Clone/Copy specifically)</p>



<a name="212590818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212590818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212590818">(Oct 07 2020 at 17:18)</a>:</h4>
<p>ah ha! <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=f826c89a9ed6e159158f9d9ec343b4b4">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=f826c89a9ed6e159158f9d9ec343b4b4</a></p>



<a name="212591137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212591137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212591137">(Oct 07 2020 at 17:21)</a>:</h4>
<p>not actually sure it works:<br>
<a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=686ed364a7971ef2bf345dfcb7d47f9d">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=686ed364a7971ef2bf345dfcb7d47f9d</a></p>



<a name="212592463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212592463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212592463">(Oct 07 2020 at 17:31)</a>:</h4>
<p>Use <code>default fn</code> instead of <code>default impl</code></p>



<a name="212593050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212593050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212593050">(Oct 07 2020 at 17:35)</a>:</h4>
<p>FWIW, I find <code>default impl</code> to lead to confusion, I wish it was named something like <code>partial impl</code>, because <code>default</code> suggests it can be overridden, and a <code>default impl</code> actually cannot. (For those wanting a <code>default impl</code>, that would be a <code>partial impl { default …, default … }</code>, which makes sense).</p>



<a name="212594887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212594887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212594887">(Oct 07 2020 at 17:50)</a>:</h4>
<p>wow, <em>that's</em> how that syntax works</p>



<a name="212600640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212600640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212600640">(Oct 07 2020 at 18:36)</a>:</h4>
<p><span class="user-mention" data-user-id="249222">@Elichai Turkel</span> The other thing that can let you do this is <a href="https://doc.rust-lang.org/nightly/unstable-book/language-features/marker-trait-attr.html">https://doc.rust-lang.org/nightly/unstable-book/language-features/marker-trait-attr.html</a> to relax the overlap rule.  That works at the cost of not letting the traits have any methods, though, so it's not a replacement for specialization.</p>



<a name="212608939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212608939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212608939">(Oct 07 2020 at 19:38)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/122651-general/topic/OR.20traits/near/212592463">said</a>:</p>
<blockquote>
<p>Use <code>default fn</code> instead of <code>default impl</code></p>
</blockquote>
<p>Nice! it works :)</p>



<a name="212627905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212627905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212627905">(Oct 07 2020 at 22:38)</a>:</h4>
<p><span class="user-mention" data-user-id="232018">@Daniel Henry-Mantilla</span> I like this!</p>



<a name="212628400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212628400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212628400">(Oct 07 2020 at 22:44)</a>:</h4>
<p>in fact if you wrote up an RFC explaining it and proposing <code>partial</code> as a keyword, I'd support it. it just seems much more self-explanatory</p>



<a name="212628557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212628557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212628557">(Oct 07 2020 at 22:45)</a>:</h4>
<p>(I'm not on the lang team to be clear)</p>



<a name="212628889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212628889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212628889">(Oct 07 2020 at 22:48)</a>:</h4>
<p>I.. think there's still time to get it in for the 2021 edition</p>



<a name="212629261"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212629261" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212629261">(Oct 07 2020 at 22:52)</a>:</h4>
<p>default fn isn't a thing on stable, right? so it's not an edition thing</p>



<a name="212629267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212629267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212629267">(Oct 07 2020 at 22:52)</a>:</h4>
<p>and all of this should be contextual keyword viable, afaict</p>



<a name="212629844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212629844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212629844">(Oct 07 2020 at 22:59)</a>:</h4>
<p>generally speaking though lang policy is that we need an active project group, and I don't <em>personally</em> think there's capacity for it (but not lang team myself)</p>



<a name="212629985"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212629985" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212629985">(Oct 07 2020 at 23:00)</a>:</h4>
<p>contextual keyword may work, but since you can define impls in function bodies I'm not sure</p>



<a name="212630050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212630050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212630050">(Oct 07 2020 at 23:01)</a>:</h4>
<p>the edition would specifically be for reserving the keyword, if it's needed</p>



<a name="212630223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212630223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212630223">(Oct 07 2020 at 23:03)</a>:</h4>
<p>sure. I imagine we can punt an edition anyway :)</p>



<a name="212630317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212630317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212630317">(Oct 07 2020 at 23:04)</a>:</h4>
<p>The edition <a href="https://github.com/nikomatsakis/rfcs/blob/edition-2021-and-beyond/text/0000-edition-2021-and-beyond.md">RFC</a> says that reserving keywords has to be done as part of an active project group, but then later says it has to be an accepted RFC. I would think either/or (and in this case, given that we're modifying one keyword of an unstable language feature, a project group seems like overkill)</p>



<a name="212630345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212630345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212630345">(Oct 07 2020 at 23:05)</a>:</h4>
<p>yeah we can, I'm just thinking we'll want to stabilize some of specialization in the next 3 years</p>



<a name="212630503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212630503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212630503">(Oct 07 2020 at 23:07)</a>:</h4>
<p>I think pinging some parser/grammar folks and checking if we can swap this keyword out seems good</p>



<a name="212630512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212630512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212630512">(Oct 07 2020 at 23:07)</a>:</h4>
<p>I wouldn't want a blanket reservation anyway, though</p>



<a name="212630568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212630568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212630568">(Oct 07 2020 at 23:08)</a>:</h4>
<p>and personally I am pretty sure that we do not have problems with contextual keywords here</p>



<a name="212630855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212630855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212630855">(Oct 07 2020 at 23:12)</a>:</h4>
<p>good to know. started a thread in <a class="stream" data-stream-id="213817" href="/#narrow/stream/213817-t-lang">#t-lang</a> to make sure</p>



<a name="212786942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/OR%20traits/near/212786942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/OR.20traits.html#212786942">(Oct 09 2020 at 06:19)</a>:</h4>
<p>I don't have any objection, with my lang hat on, but I would prefer to see specialization on a path to stabilization rather than worrying about its syntax. :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>